package com.killer.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;

/**
 * @author Killer
 * @title: FeignInterceptor
 * @projectName shop-parent
 * @description: TODO
 * @date 2021/11/1 20:44
 */
@Component
public class FeignInterceptor implements RequestInterceptor {

    /**
     * Feign之前调用拦截
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes requestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获得request中的请求投信息，放入template中
        Enumeration<String> headerNames = requestAttributes.getRequest().getHeaderNames();
        //遍历
        while (headerNames.hasMoreElements()) {
            String headerKey=headerNames.nextElement();
            //通过key获取value
            String headValue = requestAttributes.getRequest().getHeader(headerKey);
            template.header(headerKey, headValue);
        }
    }
}
